<template>
  <div class="question-card" :class="{ 'code-question': isCodeQuestion }">
    <div class="card-header">
      <div class="question-number">{{ index + 1 }}</div>
      <h3 class="question-title">{{ question.title }}</h3>
    </div>
    
    <div class="card-body">
      <!-- 选择题选项 -->
      <template v-if="!isCodeQuestion && question.options">
        <ul class="options-list">
          <li v-for="(option, idx) in question.options" :key="idx" class="option-item">
            <div class="option-marker">{{ optionLabels[idx] }}</div>
            <div class="option-content">{{ option }}</div>
          </li>
        </ul>
      </template>
      
      <!-- 代码题答案 -->
      <template v-if="isCodeQuestion">
        <div class="code-preview">
          <pre><code>{{ question.answer }}</code></pre>
        </div>
      </template>
      
      <!-- 答案部分 -->
      <div class="answer-section" v-if="showAnswer">
        <div class="answer-label">答案:</div>
        <div class="answer-content">{{ question.answer }}</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';

const props = defineProps({
  question: {
    type: Object,
    required: true
  },
  index: {
    type: Number,
    default: 0
  },
  showAnswer: {
    type: Boolean,
    default: true
  }
});

const isCodeQuestion = computed(() => {
  return !props.question.options;
});

const optionLabels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
</script>

<style scoped>
.question-card {
  margin-bottom: 1.5rem;
  border-radius: 0.75rem;
  overflow: hidden;
  transition: all 0.3s ease;
  background: white;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
  border-left: 4px solid #3b82f6;
}

.question-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

.code-question {
  border-left: 4px solid #f97316;
}

.card-header {
  padding: 1rem;
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  border-bottom: 1px solid #f3f4f6;
  background: linear-gradient(180deg, rgba(59, 130, 246, 0.05) 0%, rgba(249, 115, 22, 0.05) 100%);
}

.question-number {
  flex-shrink: 0;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 700;
  background: linear-gradient(135deg, #3b82f6 0%, #f97316 100%);
}

.code-question .question-number {
  background: linear-gradient(135deg, #f97316 0%, #3b82f6 100%);
}

.question-title {
  font-size: 1.125rem;
  font-weight: 500;
  line-height: 1.4;
  margin: 0;
}

.card-body {
  padding: 1.25rem;
}

.options-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.option-item {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  padding: 0.75rem;
  border-radius: 0.5rem;
  transition: background-color 0.2s ease;
  background-color: rgba(59, 130, 246, 0.03);
}

.option-item:hover {
  background-color: rgba(59, 130, 246, 0.08);
}

.option-marker {
  flex-shrink: 0;
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 0.875rem;
  font-weight: 500;
  background: linear-gradient(135deg, #3b82f6 0%, #60a5fa 100%);
}

.option-content {
  color: #374151;
}

.code-preview {
  padding: 1rem;
  border-radius: 0.5rem;
  overflow-x: auto;
  background-color: #f8f9fa;
  border: 1px solid #e2e8f0;
}

.code-preview pre {
  margin: 0;
  font-family: 'Courier New', monospace;
  font-size: 0.9rem;
}

.answer-section {
  margin-top: 1.25rem;
  padding-top: 1rem;
  border-top: 1px dashed #e5e7eb;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.answer-label {
  font-weight: 700;
  color: #3b82f6;
}

.answer-content {
  font-weight: 500;
  color: #f97316;
}
</style> 